from PIL import Image
from wordcloud import WordCloud
import jieba
import numpy as np
from matplotlib import pyplot as plt
from pymysql import connect

def get_img(filed, targetImageSrc, resImageSrc):
    con = connect(host='localhost', user='root', password='123456', database='cardate', port=3306, charset='utf8')
    cur = con.cursor()
    sql = f"select {filed} from carinfo"
    cur.execute(sql)
    data = cur.fetchall()

    text = ''
    for i in data:
        if i[0] != '':
            tagArr = i
            for j in tagArr:
                text += j

    cur.close()
    con.close()

    data_cut = jieba.cut(text, cut_all=False)
    string = ' '.join(data_cut)

    # 图片
    img = Image.open(targetImageSrc)
    img_arr = np.array(img)
    wc = WordCloud(
        font_path='msyh.ttc',
        mask=img_arr,
        background_color='#04122c',
    )
    wc.generate_from_text(string)

    # 绘制
    fig = plt.figure(1)
    plt.imshow(wc)
    plt.axis('off')

    plt.savefig(resImageSrc, dpi=800,bbox_inches='tight',pad_inches=-0.1)
    plt.show()

get_img('manufacturer', 'big-screen-vue-datav-master/public/carCloud.png', 'big-screen-vue-datav-master/public/car_cloud.png')
